<?php

require_once dirname(__FILE__) . '/../lib/incidentGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/incidentGeneratorHelper.class.php';

/**
 * incident actions.
 *
 * @package    maslogger
 * @subpackage incident
 * @author     Michaël VEROUX
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class incidentActions extends autoIncidentActions {

    public function executeListToFit(sfWebRequest $request) {
        $this->ml_incidents = $this->getRoute()->getObject();
        $fit = new MlFit();
        $txt = $fit->setFitRecord($this->ml_incidents);
        if ($request->isXmlHttpRequest()) {
            $this->getResponse()->setContentType('text/plain');
            return $this->renderText($txt);
        } else {
            $this->redirect('@ml_incidents');
        }
    }

    public function executeDelete(sfWebRequest $request) {
        $this->ml_incidents = $this->getRoute()->getObject();
        $fit = new MlFit();
        $txt = $fit->delFitRecord($this->ml_incidents);
        return parent::executeDelete($request);
    }

    public function executeAjaxSelect(sfWebRequest $request) {
        $this->getResponse()->setContentType('application/json');

        $string = $request->getParameter('q');

        $req = Doctrine::getTable('MlMachines')->getAjaxSearch($string);

        $results = array();
        foreach ($req as $result)
            $results[$result->getId()] = $result->getMachineStringFull();

        if (count($results) <= 0) {
            $results[0] = 'Aucun résultat !';
        }

        return $this->renderText(json_encode($results));
    }

    private function setFilterMachine($request) {
        $number = intval($request->getParameter("numero"));
        $machine = Doctrine_Core::getTable('MlMachines')->find($number);

        $incident = new MlIncidents();
        $incident->setMachineId($number);
        $incident->setVersionId($machine['version']);

        $this->form = new MlIncidentsForm($incident);
        $this->ml_incidents = $this->form->getObject();
        unset($this->form['suggest_box']);

        /*         * ********************************* POUR LA LISTE ******************************************* */
        $filtre = array();
        $filtre = $request->getParameter('ml_incidents_filters');

        $filtre['machine_id']['text'] = $machine->getNumero();
        $request->setParameter('ml_incidents_filters', $filtre);
        $this->is_partial = true;
        $this->executeFilter($request);
        /*         * ********************************************************************************************** */
    }

    public function executeDeclare(sfWebRequest $request) {
        //$this->forward404unless($request->isXmlHttpRequest());
        $this->setFilterMachine($request);
        return $this->renderPartial('new', array('form' => $this->form, 'pager' => $this->pager, 'sort' => $this->sort, 'ml_incidents' => $this->ml_incidents, 'configuration' => $this->configuration, 'helper' => $this->helper));
    }

    public function executeTabs(sfWebRequest $request) {
        $tab = $request->getParameter("tab");
        if ($tab == '') {
            $this->setFilters($this->configuration->getFilterDefaults());
            $tab = 'incident';
        }
        $filtre = array();
        $filtre = $request->getParameter('ml_incidents_filters');
        if ($tab == 'historique') {
            $filtre['priorite'] = '';
        }
        if ($tab == 'incident') {
            $filtre['priorite'] = 'A faire';
        }
        $request->setParameter('ml_incidents_filters', $filtre);
        $this->executeFilter($request);
    }

    public function executeFilter(sfWebRequest $request) {
        $filtre = $request->getParameter('ml_incidents_filters');
        if (isset($filtre['machine_id']['text'])) {
            $machine = Doctrine::getTable('MlMachines')->findOneByNumero((int) $filtre['machine_id']['text']);
            if ($machine) {
                $filtre['machine_id'] = $machine->getId();
            } else {
                $filtre['machine_id'] = '';
            }
            $request->setParameter('ml_incidents_filters', $filtre);
        }
        $this->setPage(1);

        if ($request->hasParameter('_reset')) {
            $this->setFilters($this->configuration->getFilterDefaults());

            if (!isset($this->is_partial)) {
                $this->redirect('@ml_incidents');
            }
        }

        $this->filters = $this->configuration->getFilterForm($this->getFilters());

        $this->filters->bind($request->getParameter($this->filters->getName()));
        if ($this->filters->isValid()) {
            $this->setFilters($this->filters->getValues());

            if (!isset($this->is_partial)) {
                $this->redirect('@ml_incidents');
            }
        }

        $this->pager = $this->getPager();
        $this->sort = $this->getSort();
        if (!isset($this->is_partial)) {
            $this->setTemplate('index');
        }
    }

    public function executeIndex(sfWebRequest $request) {
        if ($request->getParameter('partial') == 'true') {
            $this->setFilterMachine($request);
        }
        parent::executeIndex($request);

        if ($request->getParameter('partial') == 'true') {
            return $this->renderPartial('historique', array('pager' => $this->pager, 'sort' => $this->sort, 'helper' => $this->helper, 'historique' => true));
        }
    }

    public function executeEdit(sfWebRequest $request) {
        parent::executeEdit($request);
        $historique = new historiqueIncident($this->form->getObject()->getId());
        $this->changements = $historique->getChangementsAsArray();
        if (!$this->getUser()->hasCredential('Slot')) {
            $this->form->disableIntervention();
            $this->form->disableCheckbox();
        }
    }
  public function executeUpdate(sfWebRequest $request)
  {
    $this->ml_incidents = $this->getRoute()->getObject();
    $this->form = $this->configuration->getForm($this->ml_incidents);
        if (!$this->getUser()->hasCredential('Slot')) {
            $this->form->disableCheckbox();
        }
    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }    

}
